package com.idione.model;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.idione.activities.R;
import com.idione.util.LogUtil;

public class ImageAdapter extends BaseAdapter {

	public final static int SIGN_REGULATORY = 0;
	public final static int SIGN_WARNING = 1;
	public final static int SIGN_INFORMATIVE = 2;
	public final static int SIGN_TRANSIENT = 3;

	private int signs;
	private Context context;
	private StructImg[] images;

	// references to our images
	private int[][] idSignals = {
			{ // SIGN_REGULATORY
				R.drawable.imgsignal1, R.drawable.imgsignal2, R.drawable.imgsignal3,
				R.drawable.imgsignal4, R.drawable.imgsignal5, R.drawable.imgsignal6,
				R.drawable.imgsignal7, R.drawable.imgsignal8, R.drawable.imgsignal9,
				R.drawable.imgsignal10, R.drawable.imgsignal11, R.drawable.imgsignal12,
				R.drawable.imgsignal13, R.drawable.imgsignal14, R.drawable.imgsignal15,
				R.drawable.imgsignal16,	R.drawable.imgsignal17,	R.drawable.imgsignal18,
				R.drawable.imgsignal19,	R.drawable.imgsignal20,	R.drawable.imgsignal21,
				R.drawable.imgsignal22,	R.drawable.imgsignal23,	R.drawable.imgsignal24,
				R.drawable.imgsignal25,	R.drawable.imgsignal26,	R.drawable.imgsignal27,
				R.drawable.imgsignal28,	R.drawable.imgsignal29,	R.drawable.imgsignal30,
				R.drawable.imgsignal31,	R.drawable.imgsignal32,	R.drawable.imgsignal33,
				R.drawable.imgsignal34,	R.drawable.imgsignal35,	R.drawable.imgsignal36,
				R.drawable.imgsignal37,	R.drawable.imgsignal38,	R.drawable.imgsignal39,
				R.drawable.imgsignal40,	R.drawable.imgsignal41,	R.drawable.imgsignal42,				
				R.drawable.imgsignal43,	R.drawable.imgsignal44,	R.drawable.imgsignal45,
				R.drawable.imgsignal46,	R.drawable.imgsignal47, R.drawable.imgsignal48,
				R.drawable.imgsignal49,	R.drawable.imgsignal50,	R.drawable.imgsignal51,	
				R.drawable.imgsignal52, R.drawable.imgsignal53, R.drawable.imgsignal54,
				R.drawable.imgsignal55, R.drawable.imgsignal56, R.drawable.imgsignal57,
			},
			{ // SIGN_WARNING
				R.drawable.imgsignal58, R.drawable.imgsignal59, R.drawable.imgsignal60,
				R.drawable.imgsignal61, R.drawable.imgsignal62,	R.drawable.imgsignal63,
				R.drawable.imgsignal64, R.drawable.imgsignal65,	R.drawable.imgsignal66,
				R.drawable.imgsignal67,	R.drawable.imgsignal68,	R.drawable.imgsignal69,
				R.drawable.imgsignal70,	R.drawable.imgsignal71,	R.drawable.imgsignal72,
				R.drawable.imgsignal73, R.drawable.imgsignal74,	R.drawable.imgsignal75,
				R.drawable.imgsignal76,	R.drawable.imgsignal77,	R.drawable.imgsignal78,
				R.drawable.imgsignal79,	R.drawable.imgsignal80,	R.drawable.imgsignal81,
				R.drawable.imgsignal82,	R.drawable.imgsignal83,	R.drawable.imgsignal84,
				R.drawable.imgsignal85,	R.drawable.imgsignal86,	R.drawable.imgsignal87,
				R.drawable.imgsignal88,	R.drawable.imgsignal89,	R.drawable.imgsignal90,
				R.drawable.imgsignal91,	R.drawable.imgsignal92,	R.drawable.imgsignal93,
				R.drawable.imgsignal94,	R.drawable.imgsignal95,	R.drawable.imgsignal96,
				R.drawable.imgsignal97,	R.drawable.imgsignal98,	R.drawable.imgsignal99,
				R.drawable.imgsignal100, R.drawable.imgsignal101, R.drawable.imgsignal102, 
				R.drawable.imgsignal103, R.drawable.imgsignal104, R.drawable.imgsignal105,
				R.drawable.imgsignal106, R.drawable.imgsignal107, R.drawable.imgsignal108, 
				R.drawable.imgsignal109, R.drawable.imgsignal110, R.drawable.imgsignal111,
				R.drawable.imgsignal112, R.drawable.imgsignal113, R.drawable.imgsignal114,
				R.drawable.imgsignal115, R.drawable.imgsignal116, R.drawable.imgsignal117,
				R.drawable.imgsignal118, R.drawable.imgsignal119, R.drawable.imgsignal120,
				R.drawable.imgsignal121, R.drawable.imgsignal122, R.drawable.imgsignal123,
				R.drawable.imgsignal124, R.drawable.imgsignal125, R.drawable.imgsignal126,
				R.drawable.imgsignal127, R.drawable.imgsignal128, R.drawable.imgsignal129,
				R.drawable.imgsignal130
			},
			{ // SIGN_INFORMATIVE
				R.drawable.imgsignal131, R.drawable.imgsignal132, R.drawable.imgsignal133,
				R.drawable.imgsignal134, R.drawable.imgsignal135, R.drawable.imgsignal136,
				R.drawable.imgsignal137, R.drawable.imgsignal138, R.drawable.imgsignal139,
				R.drawable.imgsignal140, R.drawable.imgsignal141, R.drawable.imgsignal142,
				R.drawable.imgsignal143, R.drawable.imgsignal144, R.drawable.imgsignal145,
				R.drawable.imgsignal146, R.drawable.imgsignal147, R.drawable.imgsignal148,
				R.drawable.imgsignal149, R.drawable.imgsignal150, R.drawable.imgsignal151,
				R.drawable.imgsignal152, R.drawable.imgsignal153, R.drawable.imgsignal154,
				R.drawable.imgsignal155, R.drawable.imgsignal156, R.drawable.imgsignal157,
				R.drawable.imgsignal158, R.drawable.imgsignal159, R.drawable.imgsignal160,
				R.drawable.imgsignal161, R.drawable.imgsignal162, R.drawable.imgsignal163
			},
			{ // SIGN_TRANSIENT
				R.drawable.imgsignal164, R.drawable.imgsignal165, R.drawable.imgsignal166, 
				R.drawable.imgsignal167, R.drawable.imgsignal168, R.drawable.imgsignal169, 
				R.drawable.imgsignal170, R.drawable.imgsignal171, R.drawable.imgsignal172, 
				R.drawable.imgsignal173, R.drawable.imgsignal174, R.drawable.imgsignal175, 
				R.drawable.imgsignal176, R.drawable.imgsignal177
			}
	};

	public ImageAdapter(Context c, int typeSign) {
		context = c;
		signs = typeSign;
		images = new StructImg[this.getCount()];

		String name, arg;

		DBAdapter db = new DBAdapter(context);
		SignSQLAdapter sign = new SignSQLAdapter(context);

		db.open();

		long t = 0;
		switch(signs) {
		case SIGN_REGULATORY:
			t = 1;
			break;
		case SIGN_WARNING:
			t = 58;
			break;			
		case SIGN_INFORMATIVE:
			t = 131;
			break;
		case SIGN_TRANSIENT:
			t = 164;
			break;
		}

		for(long i=0; i < this.getCount(); i++) {
			log("mThumbIds[" + signs + "][" + i + "]: " + idSignals[signs][(int) i]);
			log("id de imagen a cargar : " + (i+t));
			
			Cursor cursor = sign.getSign(db.getDB(), i + t);
			if (cursor.moveToFirst()){
				name = cursor.getString(cursor.getColumnIndex(SignSQLAdapter.NAME));
				arg = cursor.getString(cursor.getColumnIndex(SignSQLAdapter.ARG));
			} else {
				name = "";
				arg = "";
			}
			log("id de imagen a cargar : nombre: " + name + " y arg: " + arg);
			cursor.close();

			images[(int) i] = new StructImg(name, idSignals[signs][(int) i], arg); 
		}
		db.close();
	}

	public int getCount() {
		log("getCount()");
		return idSignals[signs].length;
	}

	public Object getItem(int position) {
		if (0 <= position && position < this.getCount())
			return images[position];
		else
			return null;
	}

	public long getItemId(int position) {
		return idSignals[signs][position];
	}

	// create a new ImageView for each item referenced by the Adapter
	public View getView(int position, View convertView, ViewGroup parent) {
		log("getView()");

		ImageView imageView;
		if (convertView == null) {  // if it's not recycled, initialize some attributes
			imageView = new ImageView(context);
			imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
			imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
			imageView.setPadding(8, 8, 8, 8);
		} else {
			imageView = (ImageView) convertView;
		}

		imageView.setImageResource(idSignals[signs][position]);
		return imageView;
	}

	/*----------------------------------------------------------------------------------*
	 * Log 																				*
	 *----------------------------------------------------------------------------------*/
	private final String LOGTAG = "LogsClass " + this.getClass().getName();
	private void log(String method) {
		if (LogUtil.modelLog)
			Log.i(LOGTAG, "... method : " + this.getClass().getSimpleName() + "." + method);
	}
}